#include "C2Curve.h"
#include "MyVertex.h"
#include "Cube.h"

using namespace CAD_CAM;

namespace Curves
{
	C2Curve::C2Curve(void)
	{

	}


	C2Curve::~C2Curve(void)
	{

	}

	void C2Curve::addBezierVertex( const QVector3D & v)
	{
		if(bezierPoints.count() < 4)
		{
			bezierPoints.push_back(MyVertex(v));
			if(bezierPoints.count() == 4) // create deBorePoints from first 4 bezier points
				deBorePoints = bezierC2ToDeboreC2(bezierPoints);
		}
		else
		{
			// v0, v1, v2 are first 3 points to create new segment (4th point is v )
			QVector3D v2 = bezierPoints[bezierPoints.count()-3].position;
			QVector3D v1 = bezierPoints[bezierPoints.count()-2].position;
			QVector3D v0 = bezierPoints[bezierPoints.count()-1].position; // new v0
			v2 = v1 + v1 - v2; // 
			v1 = v0 + v0 - v1; // new v1
			v2 = v1 + v1 - v2; // new v2

			bezierPoints.push_back(MyVertex(v1));
			bezierPoints.push_back(MyVertex(v2));
			bezierPoints.push_back(MyVertex(v));

			// adding new deBorePoint
			v0 = v + v - v2; // extension of the right bezier segment
			v1 = deBorePoints[deBorePoints.count()-1].position; // last existing deBore point
			v2 = v1 + 3 * (v0-v1); // new deBorePoint
			deBorePoints.push_back(MyVertex(v2));
		}
		refresGpuData(bezierPoints);
	}

	void C2Curve::addDeBoreVertex( const QVector3D & v)
	{
		deBorePoints.push_back(MyVertex(v));
		bezierPoints = deBoreC2ToBezierC2(deBorePoints);

		refresGpuData(bezierPoints);
	}

	void C2Curve::removeBezierVertex( int indx)
	{
		bezierPoints.remove(indx);
		refresGpuData(bezierPoints);
	}
}
